package com.erp.erp_ui.Contract_Info;

import com.erp.erp_entitys.Contract_Info.ContractInfo;
import com.erp.erp_entitys.Contract_Info.ContractProductCostInfo;
import com.erp.erp_entitys.Contract_Info.ContractProductInfo;
import com.erp.erp_entitys.Contract_Info.ContractProductPartsInfo;
import com.erp.erp_servers.Contract_Info.ContractInfoService;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 外销合同主表(ContractInfo)表控制层
 *
 * @author makejava
 * @since 2020-10-11 18:44:43
 */
@RestController
@RequestMapping("contractInfo")
public class ContractInfoController {
    /**
     * 服务对象
     */
    @Resource
    private ContractInfoService contractInfoService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ContractInfo selectOne(Long id) {
        return this.contractInfoService.queryById(id);
    }

    /**
     * 保存信息
     * @param record
     * @return
     */
    @PostMapping("insert")
    public R<ContractInfo> insert(@RequestBody ContractInfo record){
        try{
            return R.build(contractInfoService.insert(record));
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }


    /**
     * 修改信息
     * @param contractInfo
     * @return
     */
    @PostMapping("update")
    public R<ContractInfo> update(@RequestBody ContractInfo contractInfo){
        try{
            return R.build(contractInfoService.update(contractInfo));
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 删除信息
     * @param id
     * @return
     */
    @GetMapping("delete")
    public R<Boolean> update(@RequestParam(value="id",required= true) Long id){
        try{
            return R.build(contractInfoService.deleteById(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 删除报价产品信息
     * @param id
     * @return
     */
    @GetMapping("deleteProduct")
    public R<Boolean> deleteProduct(@RequestParam(value="id",required= true) Long id){
        try{
            return R.build(contractInfoService.deleteProduct(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 删除报价信息
     * @param id
     * @return
     */
    @GetMapping("deletePart")
    public R<Boolean> deletePart(@RequestParam(value="id",required= true) Long id){
        try{
            return R.build(contractInfoService.deletePart(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 删除报价信息
     * @param id
     * @return
     */
    @GetMapping("deleteCost")
    public R<Boolean> deleteCost(@RequestParam(value="id",required= true) Long id){
        try{
            return R.build(contractInfoService.deleteCost(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping(value = "listContractInfoByPage")
    public R selectByPage(ContractInfo record){
        try{
            PageInfo<ContractInfo> pageInfo = contractInfoService.selectContractInfoByPage(record);
            return R.build(200, "success", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 产品配件分页查询
     * @return
     */
    @GetMapping(value = "listProductByPage")
    public R selectProductsByPage(ContractProductInfo record){
        try{
            PageInfo<ContractProductInfo> pageInfo = contractInfoService.selectProductsByPage(record);
            return R.build(200, "success", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }


    /**
     * 产品配件分页查询
     * @return
     */
    @GetMapping(value = "listPartByPage")
    public R selectPartsByPage(ContractProductPartsInfo record){
        try{
            PageInfo<ContractProductPartsInfo> pageInfo = contractInfoService.selectPartsByPage(record);
            return R.build(200, "success", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 费用分页查询
     * @return
     */
    @GetMapping(value = "listCostByPage")
    public R selectCostsByPage(ContractProductCostInfo record){
        try{
            PageInfo<ContractProductCostInfo> pageInfo = contractInfoService.selectCostByPage(record);
            return R.build(200, "success", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

}